package cqrtplm.aop;

import cn.hutool.core.collection.CollUtil;
import com.github.pagehelper.PageInfo;
import com.hustcad.plm.pdm.common.model.dto.search.SearchAttributeDTO;
import com.hustcad.plm.pdm.common.model.dto.search.SearchAttributeGroupDTO;
import com.hustcad.plm.pdm.common.model.dto.search.SearchConditionDTO;
import com.hustcad.plm.pdm.common.model.dto.search.SearchTypeDTO;
import com.hustcad.plm.pdm.common.model.vo.search.ResultEntityVO;
import com.hustcad.plm.pdm.enumeration.service.TyplmEnumerationService;
import com.hustcad.plm.pdm.iba.service.TyplmAttributeValueService;
import com.hustcad.plm.pdm.user.service.TyplmUserService;
import com.ty.basic.entity.org.UserDO;
import com.ty.basic.vo.AttributeVO;
import cqrtplm.mapper.CADDocumentMapper;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.math.BigInteger;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

@Aspect
@Component
@Slf4j
public class QueryByConditionAOP {


    @Resource
    private CADDocumentMapper cadDocumentMapper;
    @Resource
    private TyplmUserService typlmUserService;
    @Resource
    private TyplmAttributeValueService typlmAttributeValueService;
    @Resource
    private TyplmEnumerationService typlmEnumerationService;
    /**
     * 查询指定业务版本的CAD信息并返回
     * @param point
     * @return
     * @throws Throwable
     * @auth
     */
    @SneakyThrows
    @Around(value = "execution(* com.hustcad.plm.pdm.common.service.search.TyplmCommonSearchService.queryByConditionForPage(..))")
    public Object checkWhenCreateChangeRequest(ProceedingJoinPoint point) {
        Object[] args = point.getArgs();
        Object proceed = point.proceed();
        SearchConditionDTO dto = (SearchConditionDTO)args[0];
        SearchTypeDTO searchTypeDTO = dto.getTypeList().get(0);
        if("ty.inteplm.cad.CTyCADDoc|二维图纸|AutoCAD".equalsIgnoreCase(searchTypeDTO.getTypeInthid()) && CollUtil.isNotEmpty(dto.getConditionGroups())){
            SearchAttributeGroupDTO conditionGroups = dto.getConditionGroups().get(0);
            if (CollUtil.isNotEmpty(conditionGroups.getConditions())) {
                SearchAttributeDTO conditions = conditionGroups.getConditions().get(0);
                String propertyName = conditions.getPropertyName();
                if("BusinessVersion".equals(propertyName)){
                    String keyword = dto.getKeyword();
                    String propertyValue = conditions.getPropertyValue();
                    List<ResultEntityVO> list = cadDocumentMapper.getHistoryCadInfo(keyword, propertyValue);
                    List<BigInteger> ids = list.stream().map(ResultEntityVO::getOid).collect(Collectors.toList());
                    Map<String, String> enmumap = this.typlmEnumerationService.getEnabledEnumItemsMap(Collections.singletonList("LifeCycleState")).get("LifeCycleState");
                    Map<String, List<AttributeVO>> ibaMap = typlmAttributeValueService.getAttrValueTableByEntitys(ids, "ty.inteplm.cad.CTyCADDoc");
                    Set<BigInteger> userIds = new HashSet<>();
                    for (ResultEntityVO resultEntityVO : list) {
                        userIds.add(resultEntityVO.getUpdatoroid());
                        userIds.add(resultEntityVO.getCreatoroid());
                    }
                    List<UserDO> userDOList = this.typlmUserService.queryUserByidList(userIds);
                    Map<BigInteger, UserDO> userDOMap = userDOList.stream().collect(
                            Collectors.toMap(UserDO::getOid, item -> item));
                    for (ResultEntityVO resultEntityVO : list) {
                        if (userDOMap.containsKey(resultEntityVO.getUpdatoroid())) {
                            UserDO userDO = userDOMap.get(resultEntityVO.getUpdatoroid());
                            resultEntityVO.setUpdatorName(userDO.getName());
                            resultEntityVO.setUpdatorFullName(userDO.getFullname());
                        }
                        if (userDOMap.containsKey(resultEntityVO.getCreatoroid())) {
                            UserDO userDO = userDOMap.get(resultEntityVO.getCreatoroid());
                            resultEntityVO.setCreatorName(userDO.getName());
                            resultEntityVO.setCreatorFullName(userDO.getFullname());
                        }
                        if (ibaMap.containsKey(resultEntityVO.getOid().toString())) {
                            resultEntityVO.setIbaAttrs(ibaMap.get(resultEntityVO.getOid().toString()));
                        }
                        if (enmumap.containsKey(resultEntityVO.getLifecyclestagekey())) {
                            resultEntityVO.setLifecyclestagekeyName(enmumap.get(resultEntityVO.getLifecyclestagekey()));
                        }
                    }
                    return new PageInfo<>(list);
                }
            }
        }
        return proceed;
    }
}

